package com.juku2024.juku

import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.EditText
import android.widget.TextView
import com.juku2024.juku.base.BaseActivity
import com.juku2024.juku.fragment.HotSearchFragment
import com.juku2024.juku.fragment.SearchResultFragment
import com.juku2024.juku.tools.AppUtils
import com.juku2024.juku.viewmodel.SearchViewModel


class SearchActivity : BaseActivity() {

    private lateinit var etSearch: EditText
    private lateinit var tvSearchCancel: TextView

    private var isClickHistory = false
    private val hotFragment by lazy { HotSearchFragment() }
    private val resultFragment by lazy { SearchResultFragment() }
    private val viewModel by lazy { getViewModel(SearchViewModel::class.java) }

    override fun getLayoutResId(): Int {
        return R.layout.activity_search
    }

    override fun initView() {
        super.initView()
        etSearch = findViewById(R.id.etSearch)
        tvSearchCancel = findViewById(R.id.tvSearchCancel)
    }
    
    override fun init(savedInstanceState: Bundle?) {
        supportFragmentManager.beginTransaction()
            .add(R.id.hotSearchLayout, hotFragment)
            .commit()
    }

    override fun bindEvent() {
        super.bindEvent()
        tvSearchCancel.setOnClickListener {
            if (hasResultFragment()) {
                supportFragmentManager.popBackStack()
                etSearch.setText("")
            } else {
                AppUtils.closeKeyboard(etSearch)
                finish()
            }
        }

        etSearch.setOnEditorActionListener { _, actionId, _ ->
            val currentSearchKeyword = etSearch.text.toString()
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                search(currentSearchKeyword)
            }
            return@setOnEditorActionListener false
        }
    }

    override fun onResume() {
        super.onResume()
        etSearch.post {
            etSearch.requestFocusFromTouch()
            AppUtils.openKeyboard(etSearch)
        }
    }

    fun search(key: String?){
        if (TextUtils.isEmpty(key)) {
            return
        }

        isClickHistory = true
        etSearch.setText(key)
        // 加入搜索历史
        viewModel.insertHistory(key)
        // 隐藏输入框
        closeKeyboard()
        // 执行搜索
        if (hasResultFragment()) {
            resultFragment.search(key!!)
        } else {
            val bundle = Bundle()
            bundle.putString("search", key)
            resultFragment.arguments = bundle
            supportFragmentManager.beginTransaction()
                .replace(R.id.hotSearchLayout, resultFragment)
                .addToBackStack("result")
                .commit()
        }

    }

    private fun hasResultFragment() = supportFragmentManager.fragments.any { it == resultFragment }

    override fun onStop() {
        super.onStop()
        closeKeyboard()
    }


    fun closeKeyboard(){
        AppUtils.closeKeyboard(etSearch)
    }

}